﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VietThinks.Contract;
using VietThinks.Model;

namespace VietThinks.Service
{
    public class AdminService:IAdminService
    {
        public IList<ConfigurationDto> GetAllConfigurations()
        {
            using (var repository = DtoRepository<ConfigurationDto, Configuration>.Open())
            {
                return repository.GetAll();
            }
        }
        #region "Role"
        public IList<RoleDto> GetAllRole()
        {
            using (var repository = DomainRepository.Open())
            {
                var listRole = repository.Query<Role>().ToList();

                return listRole.Select(c => Convert.Entity(c).ToDto<RoleDto>()).ToList();
            }
        }
        public RoleDto GetRoleById(int roleID)
        {
            using (var repository = DtoRepository<RoleDto, Role>.Open())
            {
                return repository.Get(roleID);
            }
        }
        public void DeleteRole(int roleId)
        {
            using (var repository = DomainRepository.Open())
            {
                var dto = repository.Get<Role>(roleId);
                repository.Remove(dto);
            }
        }
        public void AddOrUpdate(RoleDto dto)
        {
            using (var repository = DtoRepository<RoleDto, Role>.Open())
            {
                repository.AddOrUpdate(dto);
            }
        }
        #endregion
        #region "Right"
        public IList<RightDto> GetAllRight()
        {
            using (var repository = DomainRepository.Open())
            {
                var listRole = repository.Query<Right>().ToList();

                return listRole.Select(c => Convert.Entity(c).ToDto<RightDto>()).ToList();
            }
        }
        #endregion
        #region "RightInRole"
        public IList<RightInRoleDto> GetAllRightInRole()
        {
            using (var repository = DomainRepository.Open())
            {
                var listRole = repository.Query<RightInRole>().ToList();

                return listRole.Select(c => Convert.Entity(c).ToDto<RightInRoleDto>()).ToList();
            }
        }
        public IList<RightInRoleDto> GetRights(int roleId)
        {
            using (var repository = DomainRepository.Open())
            {
                var listRole = repository.Get<RightInRole>(r => r.Role.Id == roleId);

                return listRole.Select(c => Convert.Entity(c).ToDto<RightInRoleDto>()).ToList();
            }
        }
        public RightInRoleDto GetRightInRoleById(int rightInRole)
        {
            using (var repository = DtoRepository<RightInRoleDto, RightInRole>.Open())
            {
                return repository.Get(rightInRole);
            }
        }        
        #endregion
    }
}
